//
//  ZYComposeTextView.swift
//  CZ29WeiboZYH
//
//  Created by 沐颜澈 on 16/7/3.
//  Copyright © 2016年 muyanche. All rights reserved.
//

import UIKit

class ZYComposeTextView: UITextView {
    
    
    //重写父类已有的属性
    
    override var font: UIFont? {
        // 通过didSet监听font改变
        didSet{
            
            placeHolderLabel.font = font
        }
    
    }
    //占位文字的重写
    var placeHolder: String? {
        didSet{
            placeHolderLabel.text = placeHolder
        
        }
    
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MRAK: - 设置视图
    private func setupUI() {
    
        backgroundColor = UIColor.whiteColor()
        
        //添加控件
        addSubview(placeHolderLabel)
        
        //设置约束
        placeHolderLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(8)
            make.leading.equalTo(self).offset(5)
            make.width.equalTo(SCREENW - 10)
            
        }
        
        //通过系统的通知监听文字的改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewTextDidChange", name: UITextViewTextDidChangeNotification, object: nil)
    
    }
    
    //移除通知
    deinit {
        
        NSNotificationCenter.defaultCenter().removeObserver(self)
        
    
    }
    
    func textViewTextDidChange() {
        //print("发生改变")
        placeHolderLabel.hidden = self.hasText()
    }
    
    //MRAK: - 懒加载控件
    private lazy var placeHolderLabel: UILabel = {
        
        let lab = UILabel()
        lab.textColor = RandomColor()
        lab.font = UIFont.systemFontOfSize(18)
        //lab.text = "向天再借五百年,出了东门是西门..."
        lab.numberOfLines = 0
        return lab
    
    }()
    
    
    

    
}











